//
//  SBControlView.swift
//  sh
//
//  Created by xgp on 2017/5/7.
//  Copyright © 2017年 xyz. All rights reserved.
//

import Foundation
import SnapKit
import UIKit
class SBControlView: UIView {
    
    
    //全屏按钮
    var largeButton:UIButton?
    //进度条当前值
    var value:Float {
        get{ return  self.slider!.value}
        set{ self.slider!.value = newValue}
    }
    
    //最小值
    var minValue:Float {
        get{ return self.slider!.minimumValue}
        set{self.slider!.minimumValue = newValue}
    }
    
    //最大值
    var maxValue:Float{
        get{return self.slider!.maximumValue}
        set{self.slider!.minimumValue = newValue}
    }
    
    //当前时间
    var currentTime:String{
        get{return self.timeLabel!.text!}
        set{self.timeLabel!.text = newValue}
    }
    
    //总时间
    var totalTime:String{
        get{ return self.totalTimeLabel!.text!}
        set{ self.totalTimeLabel!.text = newValue}
    }
    //缓存条当前值
    var bufferValue:Float{
        get{ return self.bufferSlier!.value}
        set{self.bufferSlier!.value = newValue}
    }
    //UISlider手势
    var tapGesture: UITapGestureRecognizer?
    //代理方法
    var delegate:SBControlViewDelegate?
    
    
    //当前时间
    var timeLabel: UILabel?
    //总时间
    var totalTimeLabel: UILabel?
    //进度条
    var slider: UISlider?
    //缓存进度条
    var bufferSlier:UISlider?
    
    let padding:CGFloat = 8
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        timeLabel = UILabel()
        timeLabel?.textAlignment = .right
        timeLabel?.font = UIFont.systemFont(ofSize: 12)
        timeLabel?.textColor = UIColor.white
        
        //
        totalTimeLabel = UILabel()
        totalTimeLabel?.textAlignment = .left
        totalTimeLabel?.font = UIFont.systemFont(ofSize: 12)
        totalTimeLabel?.textColor = UIColor.white
        
        //
        slider = UISlider()
        slider?.setThumbImage(UIImage(named:"knob"), for: .normal)
        slider?.isContinuous = true
        self.tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
        slider?.addTarget(self, action: #selector(handleSliderPosition), for: .valueChanged)
        slider?.addGestureRecognizer(tapGesture!)
        slider?.maximumTrackTintColor = UIColor.clear
        slider?.minimumTrackTintColor = UIColor.white
        
        //
        largeButton = UIButton(type: .custom)
        largeButton?.contentMode = .scaleToFill
        largeButton?.setImage(UIImage(named:"full_screen"), for: .normal)
        largeButton?.addTarget(self, action: #selector(hanleLargeBtn), for: .touchUpInside)
        
        //
        bufferSlier = UISlider()
        bufferSlier?.setThumbImage(UIImage(), for: .normal)
        bufferSlier?.isContinuous = true
        bufferSlier?.minimumTrackTintColor = UIColor.red
        bufferSlier?.minimumValue = 0.0
        bufferSlier?.maximumValue = 1.0
        bufferSlier?.isUserInteractionEnabled = false
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func draw(_ rect: CGRect) {
        self.setupUI()
    }
    
    
    func setupUI() {
        self.addSubview(self.timeLabel!)
        self.addSubview(self.bufferSlier!)
        self.addSubview(self.slider!)
        self.addSubview(self.totalTimeLabel!)
        self.addSubview(self.largeButton!)
        
        //添加约束
        self.addConstraintsForSubviews()
        NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
    }
    
    func deviceOrientationDidChange(){
        self.addConstraintsForSubviews()
    }
    
    func addConstraintsForSubviews() -> Void {
        self.timeLabel?.snp.makeConstraints({ (make) in
            make.left.equalTo(self);
            make.bottom.equalTo(self).offset(-padding);
            make.right.equalTo(self.slider!).offset(-padding).priority(.low);
            make.width.equalTo(50);
            make.centerY.equalTo(self)
        })
        
        //后面的几个常数分别是各个控件的间隔和控件的宽度  添加自定义控件需在此修改参数
        let sliderwidth1 = kScreenWidth - padding - 50.0
        let sliderwidth2 = sliderwidth1 - 50.0 - 30.0 - padding - padding
        self.slider?.snp.makeConstraints({ (make) in
            make.left.equalTo(self.timeLabel!.snp.right).offset(padding)
            make.right.equalTo(self.totalTimeLabel!.snp.left).offset(-padding)
            make.centerY.equalTo(self)
            if(kScreenWidth<kScreenHeight){
                make.width.equalTo(sliderwidth2)
            }
        })
        
        self.totalTimeLabel?.snp.makeConstraints({ (make) in
            make.left.equalTo(self.slider!.snp.right).offset(padding)
            make.right.equalTo(self.largeButton!.snp.left)
            make.bottom.equalTo(self).offset(-padding)
            make.width.equalTo(50).priority(.high)
        })
        
        self.largeButton?.snp.makeConstraints({ (make) in
            make.bottom.right.equalTo(self).offset(-padding)
            make.left.equalTo(self.totalTimeLabel!.snp.right)
            make.width.height.equalTo(30)
        })
        
        self.bufferSlier?.snp.makeConstraints({ (make) in
            make.edges.equalTo(self.slider!)
        })
        
        self.layoutIfNeeded()
    }
    
    
    func handleTap(gestrue:UITapGestureRecognizer) -> Void {
        let point =  gestrue.location(in: self.slider)
        let pointX = point.x;
        let sliderWidth = self.slider!.frame.size.width;
        let currentValue = pointX/sliderWidth * CGFloat(self.slider!.maximumValue);
        guard let _  = delegate else {
            return;
        }
        delegate!.controlView(controlView: self, pointSliderLocationWithCurrentValue: currentValue)
    }
    
    func handleSliderPosition(slider:UISlider) -> Void {
        guard let _  = delegate else {
            return;
        }
        delegate!.controlView(controlView: self, draggedPositionWithSlider: slider)
        
    }
    
    func hanleLargeBtn(btn:UIButton) -> Void {
        guard let _  = delegate else {
            return;
        }
        delegate!.controlView(controlView: self, withLargeButton: btn)
    }
    
    
}

protocol SBControlViewDelegate  {
    /**
     点击UISlider获取点击点
     @param controlView 控制视图
     @param value 当前点击点
     */
    func controlView (controlView:SBControlView , pointSliderLocationWithCurrentValue point:CGFloat) ->Void
    
    /**
     拖拽UISlider的knob的时间响应代理方法
     @param controlView 控制视图
     @param slider UISlider
     */
    func controlView(controlView:SBControlView,draggedPositionWithSlider slider:UISlider)
    
    /**
     点击放大按钮的响应事件
     @param controlView 控制视图
     @param button 全屏按钮
     */
    func controlView  (controlView:SBControlView , withLargeButton: UIButton)
}
